# 用户历史记录
from PyQt5 import QtWidgets
from mysql import select_sql_all
import temp


class user_history(QtWidgets.QMainWindow):
    def __init__(self):
        super(user_history, self).__init__()
        self.card = temp.card

    def setupUI(self):
        self.setWindowTitle("历史记录查询")
        self.setGeometry(100, 100, 850, 850)
        self.Qtable()
        self.Qbutton()

    def Qtable(self):
        qtable = QtWidgets.QTableWidget(self)
        qtable.setColumnCount(6)
        qtable.setGeometry(20, 20, 800, 800)
        # 禁止编辑
        qtable.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        qtable.setHorizontalHeaderLabels(["序号", "卡号", "时间", "金额", "类型", "对方卡号"])
        qtable.verticalHeader().setVisible(False)
        # 查询数据，展示出来,按时间倒序显示
        sql = "SELECT * FROM ATM.recode where card = '{0}'  order by cdate desc".format(self.card)
        result = select_sql_all(sql)
        len_result = len(result)
        qtable.setRowCount(len_result)
        for i in range(len_result):
            qtable.setItem(i, 0, QtWidgets.QTableWidgetItem(str(i + 1)))
            qtable.setItem(i, 1, QtWidgets.QTableWidgetItem(str(result[i]['card'])))
            qtable.setItem(i, 2, QtWidgets.QTableWidgetItem(str(result[i]['cdate'])))
            qtable.setItem(i, 3, QtWidgets.QTableWidgetItem(str(result[i]['money'])))
            qtable.setItem(i, 4, QtWidgets.QTableWidgetItem(result[i]['status']))
            qtable.setItem(i, 5, QtWidgets.QTableWidgetItem(str(result[i]['card2'])))

    def Qbutton(self):
        qbutton = QtWidgets.QPushButton(self)
        qbutton.setText("退回")
        qbutton.setGeometry(10,10,30,30)
        qbutton.clicked.connect(self.Qbutton_click)

    def Qbutton_click(self):
        self.close()
        from user_main import user_main
        self.a = user_main()
        self.a.setupUI()
        self.a.show()